<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="/images/icon.png">


    <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link href="/dashboard.css" rel="stylesheet">

    <link href="https://cdn.jsdelivr.net/gh/gitbrent/bootstrap4-toggle@3.6.1/css/bootstrap4-toggle.min.css"
          rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/gh/gitbrent/bootstrap4-toggle@3.6.1/js/bootstrap4-toggle.min.js"></script>
</head>

<body>

<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
                    aria-expanded="false" aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">河海健康管理系统</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
            <ul class="nav navbar-nav navbar-right">
                <li><a href="/admin">图表</a></li>
                <li><a href="#">设置</a></li>
                <li><a href="/admin/checkApply">审批申请</a></li>
                <li><a href="/logout">注销</a></li>
            </ul>
        </div>
    </div>
</nav>

<div class="container-fluid">
    <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
            <ul class="nav nav-sidebar">
                <li><a href="/admin">概况</a></li>
                <li><a href="/healthAnalyse">健康分析</a></li>
                <li><a href="/labManage">实验室管理</a></li>
                <li><a href="/admin/checkApply">账号审批</a></li>
                <li class="active"><a href="/authority">权限管理<span class="sr-only">(current)</span></a></li>
                <li><a href="/admin/personal">个人中心</a></li>
                <li><a href="/QRCode">二维码生成</a></li>
            </ul>
        </div>
        <div class="container col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <h1 class="lead" style="font-size: 25px">添加管理员</h1>
            <div class="form-group">
                <label for="id">工号
                    <small style="color: red"> *</small>
                </label>
                <input type="number" name="id" class="form-control" id="id" placeholder="工号">
            </div>
            <div class="form-group">
                <label for="name">姓名
                    <small style="color: red"> *</small>
                </label>
                <input type="text" name="name" class="form-control" id="name" placeholder="姓名">
            </div>
            <div class="form-group">
                <label for="email">邮箱
                    <small style="color: red"> *</small>
                </label>
                <input type="email" name="email" class="form-control" id="email" placeholder="邮箱">
            </div>
            <div class="form-group">
                <label for="role">角色
                    <small style="color: red"> *</small>
                </label>
                <select class="form-control selectpicker" name="role" id="role">
                    <option value="admin">管理员</option>
                    <option value="teacher">教师</option>

                </select>
            </div>
            <div class="form-group">
                <label for="authority">权限
                    <small style="color: red"> *</small>
                </label>
                <select class="form-control" name="authority" id="authority">
                    <option value="p1">p1</option>
                    <option value="p2">p2</option>
                    <option value="p3">p3</option>
                </select>
            </div>
            <div class="form-group">
                <label for="password">初始密码
                    <small style="color: red"> *</small>
                </label>
                <input type="password" name="password" class="form-control" id="password" placeholder="初始密码">
            </div>
            <div class="form-group">
                <label for="password_check">确认密码
                    <small style="color: red"> *</small>
                </label>
                <input type="password" class="form-control" id="password_check" placeholder="确认密码">
            </div>
            <button onclick="submit()" class="btn btn-primary">提交</button>
        </div>
    </div>
</div>
<script>

    function submit() {
        var id = $("#id").val();

        var name = $("#name").val();

        var email = $("#email").val();
        var role = $("#role").val();

        var authority = $("#authority").val();


        var password = $("#password").val();

        var password01 = $("#password_check").val();
        if (password01 != password) {
            alert("两次密码不相等");
            return;
        }
        if (role == "teacher") {
            if (authority != "p3") {
                alert("教师只能拥有p3权限");
                return;
            }
        }

        var admin = {id: id, email: email, name: name, role: role, authority: authority, password: password};
        $.ajax({
            type: "POST",
            contentType: "application/json",
            dataType: "json",
            url: "/authority/addAdmin",
            data: JSON.stringify(admin),
            success: function (result) {
                var msg = alert(result);
                if (msg != "success") {
                    alert(msg);
                } else {
                    window.location.replace("/authority");
                }
            },
            error: function (result) {
                var msg = (result.responseText);
                if (msg != "success") {
                    alert(msg);
                } else {
                    window.location.replace("/authority");
                }
            }
        });

    }


</script>
</body>
</html>

